﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Configuration;

namespace PassportManage
{
    public partial class Form_Setting : Form
    {
        public Form_Setting()
        {
            InitializeComponent();
        }

        private void Form_Setting_Load(object sender, EventArgs e)
        {
//             tbPillar.Text = Properties.Settings.Default.PillarCount.ToString();
//             tbLayer.Text = Properties.Settings.Default.LayerCount.ToString();
//             tbCell.Text = Properties.Settings.Default.CellCount.ToString();
//             tbGrid.Text = Properties.Settings.Default.GridCount.ToString();
//             tbRetryTimes.Text = Properties.Settings.Default.RetryTimes.ToString();
//             tbTimeOut.Text = Properties.Settings.Default.TimeOut.ToString();
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comboPortName.Items.Clear();
            comboPortName.Items.AddRange(ports);
            comboPortName.SelectedIndex = comboPortName.Items.IndexOf(BLL.Properties.Settings.Default.PassportPortName);
            tbTimeOut.Text = BLL.Properties.Settings.Default.TimeOut.ToString();
            tbPillar.Text = BLL.Properties.Settings.Default.PillarCount.ToString();            
            textBox2.Text = ConfigurationManager.ConnectionStrings["LocalSqlConnection"].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!WFNetLib.Strings.StringsFunction.IsNum(tbPillar.Text))
            {
                MessageBox.Show("架子数量必须为正整数，当前输入不符合规范，请检查！");
                return;
            }
            if (!WFNetLib.Strings.StringsFunction.IsNum(tbTimeOut.Text))
            {
                MessageBox.Show("超时时间必须为正整数，当前输入不符合规范，请检查！");
                return;
            }                
            BLL.Properties.Settings.Default.PillarCount = byte.Parse(tbPillar.Text);
            BLL.Properties.Settings.Default.TimeOut = uint.Parse(tbTimeOut.Text);
            BLL.Properties.Settings.Default.PassportPortName = comboPortName.Text;            
            BLL.Properties.Settings.Default.Save();
//            if(textBox1)
//             try
//             {
//                 COM_Passport.Open();
//             }
//             catch (Exception ex)
//             {
//                 //捕获到异常信息，创建一个新的comm对象，之前的不能用了。
//                 COM_Passport = new SerialPort();
//                 //现实异常信息给客户。
//                 MessageBox.Show(ex.Message);
//                 MessageBox.Show("串口打开失败，无法进行护照的存入和取出记录工作！");
//             }
            MessageBox.Show("修改成功!");
            this.Close();
        }
    }
}
